package com.kongge.viewdisplayframeworklib.landscape;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;

import com.kongge.viewdisplayframeworklib.model.AbsUIController;
import com.kongge.viewdisplayframeworklib.model.RuntimeManager;
import com.kongge.viewdisplayframeworklib.util.EQConstans;
import com.kongge.viewdisplayframeworklib.control.GlobalMagager;
import com.kongge.viewdisplayframeworklib.control.PublicInterface;
import com.kongge.viewdisplayframeworklib.event.action.EQGotoFrameAction;
import com.kongge.viewdisplayframeworklib.event.param.EQParam;
import com.kongge.viewdisplayframeworklib.model.Page;

public class LandscapeActivity extends AbstractLandscapeActivity {

	// ~常量区块
	// ========================================================

	// ~成员变量区块
	// ========================================================
	private int frameId;
	private String stockCode;
	private String stockName;
	private String stockMarket;
	private EQParam param;
	private EQGotoFrameAction action;
	private LandsUIManager manager;
	private LandsHandler handler = new LandsHandler();

	// ~构造函数区块
	// ========================================================

	// ~方法区块
	// ========================================================
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 是否初始化好了，否则进入logoActivity
//		if (init) {
//			gotoLogoActivity();
//		}
		
		Intent intent = getIntent();
		if (intent != null) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				frameId = bundle.getInt(ActivityProtocal.KEY_PARAM_LANDSCAPE_FRAMEID);
				stockCode = bundle.getString(ActivityProtocal.KEY_PARAM_STOCKCODE);
				stockName = bundle.getString(ActivityProtocal.KEY_PARAM_STOCKNAME);
				stockMarket = bundle.getString(ActivityProtocal.KEY_PARAM_STOCKMARKET);
				
				action = new EQGotoFrameAction(EQGotoFrameAction.NO_ANIMATION, frameId);
				EQParam param = new EQParam(EQParam.PARAM_TYPE_STOCKINFO, stockCode);
				action.setParam(param);
			}
		}
		
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	private void init() {
		PublicInterface.setGlobalActivity(this);
		setTabControl();
		PublicInterface.gotoPageWithPageId(action);
	}
	
	private void gotoLogoActivity() {
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (PublicInterface.onKeyDown(keyCode, event)) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
	}
	
	/**
	 * 切换到横屏的操作
	 */
	private void onChangeToPortrait() {
		AbsUIController focusPage = manager.getCurPageControl();
		int landPageId = focusPage.getPageId();
		int portaitPageId = EQScreenManager.getScreenMappingPage(landPageId, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		if (portaitPageId == EQConstans.INVALID_ID) {
			portaitPageId = landPageId;
		}
		
		// 判断是否具有横屏
		if (portaitPageId != EQConstans.INVALID_ID) {
			// 通知转屏并且保存页面数据，需要保存数据的页面必须实现ISavePageState接口
			if (focusPage != null) {
				focusPage.broadcastScreenChange();
			}
			manager.mStack.removeAllStack();
			
			// 返回竖屏处理
			Intent result = new Intent();
			Bundle bundle = new Bundle();
			bundle.putInt(ActivityProtocal.KEY_PARAM_PORTRAIT_FRAMEID, portaitPageId);
			RuntimeManager runtimeManager = GlobalMagager.getRuntimeManager();
			EQGotoFrameAction action = (EQGotoFrameAction) manager.getEqAction(landPageId);
			if (action != null) {
				action.setGotoFrameId(portaitPageId);
				EQParam temp = action.getParam();
				if (temp != null) {
					temp.setValueType(EQParam.PARAM_TYPE_LANDS_TO_PORTRAIT);
				}
			}
			runtimeManager.setAction(action);
			result.putExtras(bundle);
			setResult(ActivityProtocal.REQUEST_CODE_LANDSCAPE, result);
			if (handler != null) {
				handler.removeCallbacksAndMessages(null);
			}
			this.finish();
		} else {
			// 不应发生该问题
		}
	}
	
	private void setTabControl() {
		if (manager == null) {
			manager = new LandsUIManager();
		}
		manager.init();
		manager.setActivity(this);
		manager.setParentViewGroup(rootView);
		GlobalMagager.setTabControl(manager);
	}

	// ~静态方法区块
	// ========================================================

	// ~内部接口（类）区块
	// ========================================================
	private class LandsHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
		}
	}

}
